package com.sinoglobal.ningxia.activity.vip;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.sinoglobal.dumping.base.Dumpling_SharedPreferenceUtil;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.utils.BitmapUtiles;
import com.sinoglobal.ningxia.utils.FlyUtil;
import com.sinoglobal.ningxia.utils.ItktLog;
import com.sinoglobal.ningxia.utils.MContants;
import com.sinoglobal.ningxia.utils.SharedPreferenceUtil;
import com.sinoglobal.ningxia.utils.UserUtils;

/**
 * @author lms
 * @title: SetPersonalActivity.java
 * @package com.sinoglobal.ningxia.activity.vip
 * @description: 个人设置页面
 * @date 2014-10-17 上午10:28:34
 */
public class PersonalInfoSet extends BaseActivity implements OnClickListener {
    // 意图传递
    private Intent intent;
    // 昵称
    private TextView nickName;
    // 头像
    private ImageView photo;
    // 修改信息的布局
    private LinearLayout setInfoLayout;
    // private TextView giveRight;// 授权
    // 修改密码
    private TextView changePwd;
    // 同步分享
    private TextView synchShare;
    // 消息推送
    private TextView pushMsg;
    // 关于
    private TextView about;
    // 意见反馈
    private TextView suggestBack;
    // 退出登录
    private Button exit;
    // 上下文
    private Context context;
    // 登录的手机号
    private String username;
    // 拼接后的头像地址
    private String localUrl;
    // 头像图片的地址
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vip_set_personal);
        openChenJInShi();//开启沉浸式
        init();
        setListener();
    }

    private void setListener() {
        // giveRight.setOnClickListener(this);
        changePwd.setOnClickListener(this);
        synchShare.setOnClickListener(this);
        pushMsg.setOnClickListener(this);
        about.setOnClickListener(this);
        suggestBack.setOnClickListener(this);
        setInfoLayout.setOnClickListener(this);
        nickName.setOnClickListener(this);
        exit.setOnClickListener(this);
    }

    // 初始化
    private void init() {
        titleView.setText(R.string.set_person_topstr);
        photo = (ImageView) findViewById(R.id.setinfo_photo);
        // giveRight = (TextView) findViewById(R.id.give_right_tv);
        changePwd = (TextView) findViewById(R.id.change_password_tv);
        synchShare = (TextView) findViewById(R.id.synchronous_share_tv);
        pushMsg = (TextView) findViewById(R.id.push_message_tv);
        about = (TextView) findViewById(R.id.about_tv);
        suggestBack = (TextView) findViewById(R.id.suggest_back_tv);
        setInfoLayout = (LinearLayout) findViewById(R.id.set_person_info_toplayout);
        username = getIntent().getStringExtra("username");
        nickName = (TextView) findViewById(R.id.tv_nickname);
        exit = (Button) findViewById(R.id.btn_exit);
        context = FlyApplication.context;
        ItktLog.i("==========>>>>>>>>>" + SharedPreferenceUtil.getString(FlyApplication.context, "score"));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_nickname:// 昵称栏
                intent = new Intent(PersonalInfoSet.this, UserInfoModify.class);
                intent.putExtra("username", username);
                this.startActivity(intent);
                break;

            // case R.id.give_right_tv: break;

            case R.id.change_password_tv:// 修改密码
                FlyUtil.intentForward(PersonalInfoSet.this, PasswordChange.class);
                break;
            case R.id.synchronous_share_tv:// 同步分享
                FlyUtil.intentForward(PersonalInfoSet.this, SynchronousShare.class);
                break;
            case R.id.push_message_tv:// 消息推送
                FlyUtil.intentForward(PersonalInfoSet.this, MessagePush.class);
                break;
            case R.id.about_tv:// 关于
                FlyUtil.intentForward(PersonalInfoSet.this, About.class);
                break;
            case R.id.suggest_back_tv:// 意见反馈
                FlyUtil.intentForward(PersonalInfoSet.this, SuggestBack.class);
                break;
            case R.id.setinfo_photo:// 头像
                intent = new Intent(PersonalInfoSet.this, UserInfoModify.class);
                intent.putExtra("username", username);
                this.startActivity(intent);
                break;
            case R.id.btn_exit:// 退出登录
                FlyApplication.userId = MContants.EMPTY_STRING;// 退出登录是userId为空
                // 将登录信息保存为空值并移除记录的密码

                //判断是否自动登录
                boolean isAutoChecked = SharedPreferenceUtil.getBoolean(FlyApplication.context, "autoLogin");
                if (!isAutoChecked) {
                    SharedPreferenceUtil.saveString(FlyApplication.context, "autoPwd", "");
                }
                Logger.i("清空SP");
                UserUtils.ExitSccess();
                Dumpling_SharedPreferenceUtil.clear(mActivity);
                Intent jifen = new Intent(MContants.BROADCAST_JIFEN);
                sendBroadcast(jifen);
                finish();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        url = SharedPreferenceUtil.getString(FlyApplication.context, "photo");
        //FIXME 头像返回全路径 张杨 2014.10.29
        localUrl = url;
        try {
            nickName.setText(SharedPreferenceUtil.getString(getApplicationContext(), "nickname"));
            getBitmap(localUrl, photo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 下载并将头像转换为bitmap格式
    private void getBitmap(final String imgUrl, final ImageView iv) {

        ItktAsyncTaskWithDialog<Void, Void, Bitmap> asyncTask = new ItktAsyncTaskWithDialog<Void, Void, Bitmap>(MContants.EMPTY_STRING, false, false) {

            @Override
            public Bitmap before(Void... params) throws Exception {
                return BitmapUtiles.getNetBitmap(imgUrl, getApplicationContext());
            }

            @Override
            public void after(Bitmap result) {
                if (result != null) {
                    FlyApplication.PhotoBitmap = result;
                    // 将头像切为圆形进行显示
                    iv.setImageBitmap(BitmapUtiles.toRoundBitmap(result));
                }
            }

            @Override
            public void exception() {

            }
        };
        asyncTask.execute();
    }
}
